﻿/*
COPYRIGHT (C) 2009 EPISERVER AB

THIS FILE IS PART OF REMOTE PAGE PROVIDER FOR EPISERVER CMS.
REMOTE PAGE PROVIDER FOR EPISERVER CMS IS FREE SOFTWARE: YOU CAN REDISTRIBUTE IT AND/OR MODIFY IT UNDER THE TERMS OF THE GNU LESSER GENERAL PUBLIC LICENSE VERSION v2.1 AS PUBLISHED BY THE FREE SOFTWARE FOUNDATION.

REMOTE PAGE PROVIDER FOR EPISERVER CMS IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, BUT WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. SEE THE GNU LESSER GENERAL PUBLIC LICENSE FOR MORE DETAILS.
YOU SHOULD HAVE RECEIVED A COPY OF THE GNU LESSER GENERAL PUBLIC LICENSE ALONG WITH REMOTE PAGE PROVIDER FOR EPISERVER CMS]. IF NOT, SEE <HTTP://WWW.GNU.ORG/LICENSES/>.
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
using EPiServer.Security;
using EPiServer.Web.Hosting;
using System.Web.Hosting;
using System.IO;

namespace EPiServer.Labs.RemoteProvider
{
    public partial class RemoteProviderServer 
    {
        #region Remote File Operation Members
        RemoteVirtualPathProviderServer _remoteVirtualPathProviderServer;

        /// <summary>
        /// Initializes the virtual path proivder.
        /// </summary>
        /// <param name="remoteVirtualPathProvidersInfo">The remote virtual path providers info.</param>
        public void InitializeVirtualPathProivder(ref RemoteVirtualPathProvidersInfo remoteVirtualPathProvidersInfo)
        {
            try
            {
                if (_remoteVirtualPathProviderServer == null)
                {
                    _remoteVirtualPathProviderServer = new RemoteVirtualPathProviderServer();
                }
                _remoteVirtualPathProviderServer.IntializeVirtualPathProvidersInfo(ref remoteVirtualPathProvidersInfo);
            }
            catch (Exception ex)
            {
                HandleApplicationException(ex);
            }
        }

        /// <summary>
        /// Gets the file.
        /// </summary>
        /// <param name="virtualPath">The virtual path.</param>
        /// <param name="remoteFileInfo">The remote file info.</param>
        /// <returns></returns>
        public bool GetFile(string virtualPath, out RemoteFileInfo remoteFileInfo)
        {
            try
            {
                return RemoteVirtualPathProviderServer.GetFile(virtualPath, out remoteFileInfo);
            }
            catch (Exception ex)
            {
                HandleApplicationException(ex);
            }
            remoteFileInfo = null;
            return false;
        }

        /// <summary>
        /// Gets the directory.
        /// </summary>
        /// <param name="virtualPath">The virtual path.</param>
        /// <param name="remoteDirectoryInfo">The remote directory info.</param>
        /// <returns></returns>
        public bool GetDirectory(string virtualPath, out RemoteDirectoryInfo remoteDirectoryInfo)
        {
            try
            {
                return RemoteVirtualPathProviderServer.GetDirectory(virtualPath, out remoteDirectoryInfo);
            }
            catch (Exception ex)
            {
                HandleApplicationException(ex);
            }
            remoteDirectoryInfo = null;
            return false;
        }

        /// <summary>
        /// Files the exists.
        /// </summary>
        /// <param name="virtualPath">The virtual path.</param>
        /// <returns></returns>
        public bool FileExists(string virtualPath)
        {
           try
            {
                return RemoteVirtualPathProviderServer.FileExists(virtualPath);
            }
           catch (Exception ex)
           {
               HandleApplicationException(ex);
           }
           return false;
        }

        /// <summary>
        /// Directories the exists.
        /// </summary>
        /// <param name="virtualPath">The virtual path.</param>
        /// <returns></returns>
        public bool DirectoryExists(string virtualPath)
        {
            try
            {
                return RemoteVirtualPathProviderServer.DirectoryExists(virtualPath);
            }
            catch (Exception ex)
            {
                HandleApplicationException(ex);
            }
            return false;
        }

        /// <summary>
        /// Opens the specified virtual path.
        /// </summary>
        /// <param name="virtualPath">The virtual path.</param>
        /// <param name="remoteFileInfo">The remote file info.</param>
        public void Open(string virtualPath, out RemoteFileInfo remoteFileInfo)
        {
            remoteFileInfo = null;
           try
            {
                RemoteVirtualPathProviderServer.Open(virtualPath, out remoteFileInfo);
            }
           catch (Exception ex)
           {
               HandleApplicationException(ex);
           }
        }

        /// <summary>
        /// Deletes the specified virtual path.
        /// </summary>
        /// <param name="virtualPath">The virtual path.</param>
        public void Delete(string virtualPath)
        {
            try
            {
                RemoteVirtualPathProviderServer.Delete(virtualPath);
            }
            catch (Exception ex)
            {
                HandleApplicationException(ex);
            }
        }

        /// <summary>
        /// Directorieses the specified virtual path.
        /// </summary>
        /// <param name="virtualPath">The virtual path.</param>
        /// <returns></returns>
        public RemoteDirectoryInfo[] Directories(string virtualPath)
        {
            try
            {
                return RemoteVirtualPathProviderServer.Directories(virtualPath);
            }
            catch (Exception ex)
            {
                HandleApplicationException(ex);
            }
            return null;
        }

        /// <summary>
        /// Fileses the specified virtual path.
        /// </summary>
        /// <param name="virtualPath">The virtual path.</param>
        /// <returns></returns>
        public RemoteFileInfo[] Files(string virtualPath)
        {
            try
            {
                return RemoteVirtualPathProviderServer.Files(virtualPath);
            }
            catch (Exception ex)
            {
                HandleApplicationException(ex);
            }
            return null;
        }

        /// <summary>
        /// Resolves the file by GUID.
        /// </summary>
        /// <param name="guid">The GUID.</param>
        /// <returns></returns>
        public string ResolveFileByGuid(Guid guid)
        {
            try
            {
                return RemoteVirtualPathProviderServer.ResolveFileByGuid(guid);
            }
            catch (Exception ex)
            {
                HandleApplicationException(ex);
            }
            return null;
        }

        /// <summary>
        /// Resolves the file by virtual path.
        /// </summary>
        /// <param name="virtualPath">The virtual path.</param>
        /// <param name="guid">The GUID.</param>
        /// <returns></returns>
        public string ResolveFileByVirtualPath(string virtualPath, out Guid guid)
        {
            try
            {
                return RemoteVirtualPathProviderServer.ResolveFileByVirtualPath(virtualPath, out guid);
            }
            catch (Exception ex)
            {
                HandleApplicationException(ex);
            }
            guid = Guid.Empty;
            return string.Empty;
        }


        /// <summary>
        /// Queries the access.
        /// </summary>
        /// <param name="virtualpath">The virtualpath.</param>
        /// <returns></returns>
        public AccessLevel QueryAccess(string virtualpath)
        {
            throw new NotImplementedException();
        }

        #endregion

        #region Property

        /// <summary>
        /// Gets the remote virtual path provider server.
        /// </summary>
        /// <value>The remote virtual path provider server.</value>
        RemoteVirtualPathProviderServer RemoteVirtualPathProviderServer
        {
            get
            {
                if (_remoteVirtualPathProviderServer == null)
                {
                    throw new Exception("The File Server is NOT Initialized");
                }
                return _remoteVirtualPathProviderServer;
            }
        }
        #endregion

    }
}
